var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _CanvasLabel_instances, _CanvasLabel_el, _CanvasLabel_ctx, _CanvasLabel_canvas, _CanvasLabel_resizeOB, _CanvasLabel_backData, _CanvasLabel_setting, _CanvasLabel_store, _CanvasLabel_opType, _CanvasLabel_labelList, _CanvasLabel_selectLabelIds, _CanvasLabel_labelIngData, _CanvasLabel_eventFun, _CanvasLabel_drawPipeLine, _CanvasLabel_onEvent, _CanvasLabel_keyDownEvent, _CanvasLabel_keyUpEvent, _CanvasLabel_addDrawing, _CanvasLabel_addDrawingEnd, _CanvasLabel_getLabelSetting, _CanvasLabel_drawBox, _CanvasLabel_dp, _CanvasLabel_drawPath, _CanvasLabel_dpi, _CanvasLabel_drawPoint, _CanvasLabel_drawLocation, _CanvasLabel_drawMove, _CanvasLabel_drawLabel, _CanvasLabel_labelNeedHidden, _CanvasLabel_selectIngLabelIds, _CanvasLabel_labelNeedSelect, _CanvasLabel_canEditPoint, _CanvasLabel_atPointRound, _CanvasLabel_selectLabelPoint, _CanvasLabel_moveSelectLabel, _CanvasLabel_moveLabelPoint;
function deepCopy(data) {
    return JSON.parse(JSON.stringify(data));
}
function getLabelData(l) {
    if (l.type === 'box') {
        return [
            l.data[0], l.data[1],
            l.data[0] + l.data[2], l.data[1],
            l.data[0] + l.data[2], l.data[1] + l.data[3],
            l.data[0], l.data[1] + l.data[3]
        ];
    }
    else if (l.type === 'polygon') {
        return deepCopy(l.data);
    }
    return [];
}
function copyBD(b) {
    return {
        mx: b.mx,
        my: b.my,
        zoom: b.zoom
    };
}
class CanvasLabel {
    constructor(el, watchResize) {
        var _a;
        _CanvasLabel_instances.add(this);
        // 容器元素
        _CanvasLabel_el.set(this, null
        // 2d上下文
        );
        // 2d上下文
        _CanvasLabel_ctx.set(this, null
        // 画布元素
        );
        // 画布元素
        _CanvasLabel_canvas.set(this, null);
        _CanvasLabel_resizeOB.set(this, null);
        _CanvasLabel_backData.set(this, null);
        _CanvasLabel_setting.set(this, {});
        _CanvasLabel_store.set(this, {
            mouseIsDown: false,
            x: 0,
            y: 0,
            cx: 0,
            cy: 0,
            oldOpType: 'none',
            hiddenLabelTypeIds: [],
            hiddenLabelIds: [],
            useLabelTypeId: [1]
        }
        // 操作类型
        );
        // 操作类型
        _CanvasLabel_opType.set(this, 'none'
        // 所有标签数据
        );
        // 所有标签数据
        _CanvasLabel_labelList.set(this, []);
        _CanvasLabel_selectLabelIds.set(this, []
        // 正在标注的数据
        );
        // 正在标注的数据
        _CanvasLabel_labelIngData.set(this, null);
        _CanvasLabel_eventFun.set(this, () => { });
        _CanvasLabel_selectIngLabelIds.set(this, []
        /**
         * 检查路径是否被选择 并且将被选择的id 放入到 选择列表
         * @returns
         */
        );
        // 可以编辑的点
        _CanvasLabel_canEditPoint.set(this, []
        // 检测鼠标位置是否在某个周围
        );
        __classPrivateFieldSet(this, _CanvasLabel_el, document.querySelector(el), "f");
        let canvas = document.createElement('canvas');
        canvas.innerHTML = '你的浏览器不支持canvas';
        canvas.style.userSelect = 'none';
        __classPrivateFieldSet(this, _CanvasLabel_ctx, canvas.getContext('2d'), "f");
        __classPrivateFieldSet(this, _CanvasLabel_canvas, canvas, "f");
        // 隐藏鼠标
        canvas.style.cursor = "none";
        (_a = __classPrivateFieldGet(this, _CanvasLabel_el, "f")) === null || _a === void 0 ? void 0 : _a.appendChild(canvas);
        // 调整窗口大小
        if (__classPrivateFieldGet(this, _CanvasLabel_el, "f")) {
            if (watchResize) {
                let resizeob = new ResizeObserver(() => {
                    this.reSize();
                });
                __classPrivateFieldSet(this, _CanvasLabel_resizeOB, resizeob, "f");
                resizeob.observe(__classPrivateFieldGet(this, _CanvasLabel_el, "f"));
            }
            else {
                this.reSize();
            }
        }
        this.setSetting();
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_onEvent).call(this);
    }
    /**
     * 重置画布大小
     * @param w
     * @param h
     */
    reSize(w, h) {
        if (__classPrivateFieldGet(this, _CanvasLabel_canvas, "f") && __classPrivateFieldGet(this, _CanvasLabel_el, "f")) {
            if (w !== undefined && h !== undefined) {
                __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width = w;
                __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height = h;
            }
            else {
                __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width = __classPrivateFieldGet(this, _CanvasLabel_el, "f").clientWidth;
                __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height = __classPrivateFieldGet(this, _CanvasLabel_el, "f").clientHeight;
            }
        }
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    }
    /**
     * 重绘
     */
    reDraw() {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    }
    /**
     * 删除标签
     * @param id 标签id
     */
    delLabel(id) {
        const fun = (id) => {
            for (let i = 0; i < __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").length; i++) {
                if (__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")[i].id === id) {
                    __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").splice(i, 1);
                }
            }
        };
        if (Array.isArray(id)) {
            id.forEach((d) => {
                fun(d);
            });
        }
        else if (typeof id === 'number') {
            fun(id);
        }
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        // this.#drawLocation()
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 添加设置
     * @param st 设置
     */
    setSetting(st) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").background = (st === null || st === void 0 ? void 0 : st.background) || '#bcffc3';
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType = (st === null || st === void 0 ? void 0 : st.showLabelTagType) || 'name';
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").labelTypeList = (st === null || st === void 0 ? void 0 : st.labelTypeList) || [{
                name: '标签1',
                id: 1,
                strokeWidth: 2,
                strokeStyle: 'yellow',
                fillStyle: '#0f04',
                fontFillStyle: 'yellow',
                font: "15px '微软雅黑'"
            }];
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").sightBead = {
            coordinatesColor: ((_a = st === null || st === void 0 ? void 0 : st.sightBead) === null || _a === void 0 ? void 0 : _a.coordinatesColor) || '#888',
            coordinatesWidth: ((_b = st === null || st === void 0 ? void 0 : st.sightBead) === null || _b === void 0 ? void 0 : _b.coordinatesWidth) || 2,
            fontColor: ((_c = st === null || st === void 0 ? void 0 : st.sightBead) === null || _c === void 0 ? void 0 : _c.fontColor) || '#ff6',
            fontStyle: ((_d = st === null || st === void 0 ? void 0 : st.sightBead) === null || _d === void 0 ? void 0 : _d.fontStyle) || '10px "微软雅黑"'
        };
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle = {
            strokeStyle: ((_e = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _e === void 0 ? void 0 : _e.strokeStyle) || "rgba(200,0,0,0.8)",
            strokeWidth: ((_f = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _f === void 0 ? void 0 : _f.strokeWidth) || 6,
            fillStyle: ((_g = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _g === void 0 ? void 0 : _g.fillStyle) || "rgba(122,0,0,.4)",
            pointFillR: ((_h = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _h === void 0 ? void 0 : _h.pointFillR) || 6,
            pointFillStyle: ((_j = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _j === void 0 ? void 0 : _j.pointFillStyle) || "rgba(0,230,25,.9)"
        };
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle = {
            strokeStyle: ((_k = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _k === void 0 ? void 0 : _k.strokeStyle) || "rgba(100,60,0,0.8)",
            strokeWidth: ((_l = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _l === void 0 ? void 0 : _l.strokeWidth) || 6,
            fillStyle: ((_m = st === null || st === void 0 ? void 0 : st.selectStyle) === null || _m === void 0 ? void 0 : _m.fillStyle) || "rgba(200,200,0,.4)"
        };
        __classPrivateFieldGet(this, _CanvasLabel_setting, "f").offKbdEvent = (st === null || st === void 0 ? void 0 : st.offKbdEvent) || false;
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    }
    /**
     * 获取设置
     *
     */
    getSetting() {
        return __classPrivateFieldGet(this, _CanvasLabel_setting, "f");
    }
    /**
     * 使用某个标签
     * @param id
     */
    useLabelType(id) {
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId = id;
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'checkLabelType', data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId) });
    }
    /**
     *
     * 获取现在正在使用的标签
     */
    getUseLabelType() {
        return deepCopy(__classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId);
    }
    onEvent(cbk) {
        __classPrivateFieldSet(this, _CanvasLabel_eventFun, cbk, "f");
    }
    offEvent() {
        __classPrivateFieldSet(this, _CanvasLabel_eventFun, () => { }, "f");
    }
    /**
     * 设置需要隐藏的标签列表
     * @param id
     */
    setHiddenLabels(ids) {
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelIds = ids;
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 删除被隐藏的标签列表中的某个标签
     * @param id
     */
    delHiddenLabel(id) {
        let index = __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelIds.indexOf(id);
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelIds.splice(index, 1);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 添加需要隐藏的标签
     * @param id
     */
    addHiddenLabel(id) {
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelIds.push(id);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 切换标签的 显示状态
     * @param id
     */
    checkHiddenLabel(id) {
        if (this.labelIsHidden(id)) {
            this.delHiddenLabel(id);
        }
        else {
            this.addHiddenLabel(id);
        }
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 设置需要隐藏的标签类型列表
     * @param id
     */
    setHiddenLabelTypes(ids) {
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelTypeIds = ids;
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 删除需要已经隐藏的标签类型
     * @param id
     */
    delHiddenLabelType(id) {
        let i = __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelTypeIds.indexOf(id);
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelTypeIds.splice(i, 1);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 添加需要隐藏的标签类型
     * @param id
     */
    addHiddenLabelType(id) {
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelTypeIds.push(id);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 切换标签类型的显示状态
     * @param id
     */
    checkHiddenLabelType(id) {
        if (this.labelTypeIsHidden(id)) {
            this.delHiddenLabelType(id);
        }
        else {
            this.addHiddenLabelType(id);
        }
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 检查某个标签是否被隐藏
     * @param id
     * @returns
     */
    labelIsHidden(id) {
        return __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelIds.indexOf(id) >= 0;
    }
    /**
     * 检查某个标签类型是否被隐藏
     * @param id
     * @returns
     */
    labelTypeIsHidden(id) {
        return __classPrivateFieldGet(this, _CanvasLabel_store, "f").hiddenLabelTypeIds.indexOf(id) >= 0;
    }
    /**
     * 检查标签类型是否正在使用
     * @param id type id
     * @returns 返回 标签类型的使用状态
     */
    labelTypeIsUse(id) {
        return __classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId.indexOf(id) >= 0;
    }
    /* 获取绘制上下文 */
    getCtx() {
        return __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    }
    /**
     * 清除指定区域的绘制 如果不传入值的话 就会清除整canvas
     * @param x 坐标
     * @param y 坐标
     * @param w 宽
     * @param h 长
     */
    clear(x, y, w, h) {
        if (!__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
            return;
        }
        if (x !== undefined && y !== undefined && w !== undefined && h !== undefined) {
            __classPrivateFieldGet(this, _CanvasLabel_ctx, "f").clearRect(x, y, w, h);
        }
        else {
            __classPrivateFieldGet(this, _CanvasLabel_ctx, "f").clearRect(0, 0, __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width, __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height);
        }
    }
    /**
     * 添加标注数据
     * @param data
     */
    addLabelData(data) {
        data = deepCopy(data);
        if (Array.isArray(data)) {
            __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").push(...data);
        }
        else {
            __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").push(data);
        }
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    }
    /**
     * 获取所有的标签
     */
    getLabelList() {
        return deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f"));
    }
    /**
     * 设置标签数据
     * @param ls 标签数组
     */
    setLabelList(ls) {
        if (Array.isArray(ls)) {
            let d = JSON.stringify(ls);
            __classPrivateFieldSet(this, _CanvasLabel_labelList, JSON.parse(d), "f");
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
            __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
        }
        else {
            throw new Error('setLabelList prama must be Array');
        }
    }
    /**
     * 检查现在的操作类型
     * @returns 操作类型
     */
    getOpType() {
        return __classPrivateFieldGet(this, _CanvasLabel_opType, "f");
    }
    getImageInfo() {
        return deepCopy(__classPrivateFieldGet(this, _CanvasLabel_backData, "f"));
    }
    /**
     * 查询框 是否拥有某标签类型
     * @param lid
     * @param tid
     * @returns
     */
    labelHasLabelType(lid, tid) {
        for (let label of __classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) {
            if (lid === label.id) {
                return label.labelTypeIds.indexOf(tid) >= 0;
            }
        }
        return false;
    }
    /**
     * 传入数据 自动判断数据是要添加到框的 标签类型还是移除
     * @param lid
     * @param tid
     */
    checkLabelLabelType(lid, tid) {
        for (let label of __classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) {
            if (lid === label.id) {
                let index = label.labelTypeIds.indexOf(tid);
                if (index >= 0) {
                    label.labelTypeIds.splice(index, 1);
                    __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
                }
                else {
                    label.labelTypeIds.push(tid);
                    __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
                }
                __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
            }
        }
    }
    // 切换 操作类型
    checkOpType(type) {
        if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
            return;
        }
        const cvs = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f");
        if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== type) {
            __classPrivateFieldGet(this, _CanvasLabel_store, "f").oldOpType = __classPrivateFieldGet(this, _CanvasLabel_opType, "f");
            if (type !== 'move') {
                __classPrivateFieldSet(this, _CanvasLabel_labelIngData, null, "f");
            }
            __classPrivateFieldSet(this, _CanvasLabel_opType, type, "f");
            __classPrivateFieldSet(this, _CanvasLabel_selectIngLabelIds, [], "f");
            __classPrivateFieldSet(this, _CanvasLabel_canEditPoint, [], "f");
            this.selectLabels([], false);
            if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'move') {
                cvs.style.cursor = 'grab';
            }
            else {
                cvs.style.cursor = 'none';
            }
            __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'checkType', data: __classPrivateFieldGet(this, _CanvasLabel_opType, "f") });
        }
    }
    returnOpType() {
        if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
            return;
        }
        const cvs = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f");
        __classPrivateFieldSet(this, _CanvasLabel_opType, __classPrivateFieldGet(this, _CanvasLabel_store, "f").oldOpType, "f");
        if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'move') {
            cvs.style.cursor = 'grab';
        }
        else {
            cvs.style.cursor = 'none';
        }
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'checkType', data: __classPrivateFieldGet(this, _CanvasLabel_opType, "f") });
    }
    /**
     * 缩放图片
     * @param s 添加倍数
     * @param x 相对于图片X轴坐标缩放
     * @param y 相对于图片Y轴坐标缩放
     * @returns
     */
    scale(s, x, y) {
        if (!__classPrivateFieldGet(this, _CanvasLabel_backData, "f") || !__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
            return;
        }
        let oz = __classPrivateFieldGet(this, _CanvasLabel_backData, "f").zoom;
        // 若是缩小的时候 缩放比例 已经很小 就不缩小
        if (s < 0 && oz + s < 0.1) {
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
            return;
        }
        if (x === undefined || y === undefined) {
            x = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width / 2;
            y = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height / 2;
        }
        let nz = (__classPrivateFieldGet(this, _CanvasLabel_backData, "f").zoom += s);
        let mx = __classPrivateFieldGet(this, _CanvasLabel_backData, "f").mx;
        let my = __classPrivateFieldGet(this, _CanvasLabel_backData, "f").my;
        __classPrivateFieldGet(this, _CanvasLabel_backData, "f").mx = mx - ((x - mx) * (nz / oz) - (x - mx));
        __classPrivateFieldGet(this, _CanvasLabel_backData, "f").my = my - ((y - my) * (nz / oz) - (y - my));
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'scale', data: copyBD(__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) });
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    }
    /**
     * 移动图像
     * @param x 水平
     * @param y 垂直
     */
    translate(x = 0, y = 0) {
        if (!__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
            return;
        }
        __classPrivateFieldGet(this, _CanvasLabel_backData, "f").my += y;
        __classPrivateFieldGet(this, _CanvasLabel_backData, "f").mx += x;
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'translate', data: copyBD(__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) });
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    }
    /**
     * 绘制背景图
     */
    drawImage(entry) {
        return new Promise((res, rej) => {
            __classPrivateFieldSet(this, _CanvasLabel_backData, null, "f");
            __classPrivateFieldSet(this, _CanvasLabel_labelList, [], "f");
            const setdata = (d) => {
                if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
                    return;
                }
                let data = { mx: 0, my: 0, w: d.width, h: d.height, data: d, zoom: 1 };
                let hz = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height / data.h;
                let wz = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width / data.w;
                data.zoom = (hz < wz ? hz : wz) - 0.05;
                data.mx = (__classPrivateFieldGet(this, _CanvasLabel_canvas, "f").width - data.zoom * data.w) / 2;
                data.my = (__classPrivateFieldGet(this, _CanvasLabel_canvas, "f").height - data.zoom * data.h) / 2;
                __classPrivateFieldSet(this, _CanvasLabel_backData, data, "f");
                this.clear();
                __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
                __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'scale', data: copyBD(__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) });
                res(null);
            };
            if (entry instanceof Image) {
                entry.onload = () => __awaiter(this, void 0, void 0, function* () {
                    let bm = yield createImageBitmap(entry);
                    setdata(bm);
                });
                entry.onerror = () => {
                    rej();
                };
                try {
                    (function () {
                        return __awaiter(this, void 0, void 0, function* () {
                            let bm = yield createImageBitmap(entry);
                            setdata(bm);
                        });
                    })();
                }
                catch (_a) {
                    rej();
                }
            }
            else if (entry instanceof ImageBitmap || entry instanceof ImageData) {
                setdata(entry);
            }
            else if (typeof entry === "string") {
                let img = new Image();
                img.onload = () => __awaiter(this, void 0, void 0, function* () {
                    let bm = yield createImageBitmap(img);
                    setdata(bm);
                });
                img.onerror = () => {
                    rej();
                };
                img.src = entry;
            }
        });
    }
    /**
     * 选择某些标签
     * @param ids 需要选中的标签
     */
    selectLabels(ids, d = true) {
        __classPrivateFieldSet(this, _CanvasLabel_selectLabelIds, [...ids], "f");
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: 'select', data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f")) });
        if (d) {
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        }
    }
    /**
     * 获取被选择的所有标签ids
     * @returns 所有被选择的id
     */
    getSelectLabelsIds() {
        return deepCopy(__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f"));
    }
    /**
     * 查询标签是否现在处于被选择状态
     * @param id 标签id
     * @returns
     */
    labelIsSelect(id) {
        return __classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").indexOf(id) >= 0;
    }
    /**
     * 移动某些标签
     * @param x x 方向移动多少
     * @param y y 方向移动多少
     */
    moveSelectLabels(x = 0, y = 0, o = true) {
        const sLs = __classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f");
        __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").forEach((l) => {
            if (sLs.indexOf(l.id) >= 0) {
                if (l.type === "box") {
                    l.data[0] = l.data[0] + x;
                    l.data[1] = l.data[1] + y;
                }
                else if (l.type === 'polygon') {
                    let data = [];
                    l.data.forEach((d, i) => {
                        if ((i & 1) == 0) {
                            data.push(d + x);
                        }
                        else {
                            data.push(d + y);
                        }
                    });
                    l.data = data;
                }
            }
        });
        if (o) {
            __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
        }
    }
    /**
     * 销毁 移除事件绑定
     */
    des() {
        var _a, _b;
        removeEventListener('keydown', __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_keyDownEvent));
        removeEventListener('keyup', __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_keyUpEvent));
        (_a = __classPrivateFieldGet(this, _CanvasLabel_resizeOB, "f")) === null || _a === void 0 ? void 0 : _a.unobserve(__classPrivateFieldGet(this, _CanvasLabel_el, "f"));
        (_b = __classPrivateFieldGet(this, _CanvasLabel_resizeOB, "f")) === null || _b === void 0 ? void 0 : _b.disconnect();
    }
}
_CanvasLabel_el = new WeakMap(), _CanvasLabel_ctx = new WeakMap(), _CanvasLabel_canvas = new WeakMap(), _CanvasLabel_resizeOB = new WeakMap(), _CanvasLabel_backData = new WeakMap(), _CanvasLabel_setting = new WeakMap(), _CanvasLabel_store = new WeakMap(), _CanvasLabel_opType = new WeakMap(), _CanvasLabel_labelList = new WeakMap(), _CanvasLabel_selectLabelIds = new WeakMap(), _CanvasLabel_labelIngData = new WeakMap(), _CanvasLabel_eventFun = new WeakMap(), _CanvasLabel_selectIngLabelIds = new WeakMap(), _CanvasLabel_canEditPoint = new WeakMap(), _CanvasLabel_instances = new WeakSet(), _CanvasLabel_drawPipeLine = function _CanvasLabel_drawPipeLine() {
    var _a;
    if (__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") && __classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
        let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
        let cvs = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f");
        ctx.save();
        this.clear();
        ctx.fillStyle = ((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _a === void 0 ? void 0 : _a.background) || '#fff';
        ctx.fillRect(0, 0, cvs.width, cvs.height);
        ctx.restore();
        ctx.save();
        if (__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
            let d = __classPrivateFieldGet(this, _CanvasLabel_backData, "f");
            ctx.transform(d.zoom, 0, 0, d.zoom, d.mx, d.my);
            ctx.drawImage(d.data, 0, 0, d.w, d.h);
            let line = __classPrivateFieldGet(this, _CanvasLabel_labelList, "f");
            line.forEach((item, index) => {
                __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLabel).call(this, item, index + 1);
            });
            let labeling = __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f");
            if (labeling) {
                __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLabel).call(this, labeling, 0);
            }
        }
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPoint).call(this);
        ctx.restore();
    }
}, _CanvasLabel_onEvent = function _CanvasLabel_onEvent() {
    if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f") || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f")) {
        return;
    }
    const cvs = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f");
    let that = this;
    function saveMouseP(e) {
        if (!__classPrivateFieldGet(that, _CanvasLabel_backData, "f")) {
            return;
        }
        // 鼠标坐标 
        let x = e.offsetX;
        let y = e.offsetY;
        __classPrivateFieldGet(that, _CanvasLabel_store, "f").x = x;
        __classPrivateFieldGet(that, _CanvasLabel_store, "f").y = y;
        let bd = __classPrivateFieldGet(that, _CanvasLabel_backData, "f");
        // 鼠标坐标 缩放过后再图片上的坐标
        __classPrivateFieldGet(that, _CanvasLabel_store, "f").cx = (x - bd.mx) / bd.zoom;
        __classPrivateFieldGet(that, _CanvasLabel_store, "f").cy = (y - bd.my) / bd.zoom;
        let eve = {
            cx: __classPrivateFieldGet(that, _CanvasLabel_store, "f").cx,
            cy: __classPrivateFieldGet(that, _CanvasLabel_store, "f").cy,
            x: __classPrivateFieldGet(that, _CanvasLabel_store, "f").x,
            y: __classPrivateFieldGet(that, _CanvasLabel_store, "f").y
        };
        __classPrivateFieldGet(that, _CanvasLabel_eventFun, "f").call(that, { type: "hover", data: eve });
    }
    cvs.addEventListener('wheel', (e) => {
        e.preventDefault();
        if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
            return;
        }
        let x = e.offsetX;
        let y = e.offsetY;
        if (e.altKey) {
            // 水平移动
            e.deltaY < 0 ? this.translate(20) : this.translate(-20);
        }
        else if (e.ctrlKey) {
            // 缩放
            e.deltaY < 0 ? this.scale(0.1, x, y) : this.scale(-0.1, x, y);
        }
        else {
            // 垂直移动
            e.deltaY < 0 ? this.translate(0, 20) : this.translate(0, -20);
        }
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedSelect).call(this, 'ing');
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLocation).call(this);
    });
    cvs.addEventListener('mouseover', (e) => {
        saveMouseP(e);
    });
    cvs.addEventListener('mousemove', (e) => {
        saveMouseP(e);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedSelect).call(this, 'ing');
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_addDrawing).call(this, 'mousemove');
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_selectLabelPoint).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_moveLabelPoint).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_moveSelectLabel).call(this, e.movementX, e.movementY);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawMove).call(this, e.movementX, e.movementY);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLocation).call(this);
    });
    cvs.addEventListener('mouseout', () => {
        __classPrivateFieldSet(this, _CanvasLabel_selectIngLabelIds, [], "f");
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    });
    addEventListener('keydown', __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_keyDownEvent).bind(this));
    addEventListener('keyup', __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_keyUpEvent).bind(this));
    cvs.addEventListener('mousedown', (e) => {
        saveMouseP(e);
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown = !__classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown;
    });
    cvs.addEventListener('click', () => {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_addDrawing).call(this, 'click');
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedSelect).call(this, 'entry');
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLocation).call(this);
    });
    /* 双击事件 主要用于 提交 闭合 多边形的路径 */
    cvs.addEventListener('dblclick', () => {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_addDrawingEnd).call(this, 'dblclick');
    });
    /* 鼠标抬起时间 主要用于 提交 闭合 方框路径 */
    cvs.addEventListener('mouseup', (e) => {
        saveMouseP(e);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_addDrawingEnd).call(this, 'mouseup');
        __classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown = false;
        __classPrivateFieldGet(this, _CanvasLabel_eventFun, "f").call(this, { type: "drawEnd", data: deepCopy(__classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) });
    });
}, _CanvasLabel_keyDownEvent = function _CanvasLabel_keyDownEvent(e) {
    if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
        return;
    }
    if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").offKbdEvent) {
        return;
    }
    if (e.key === 'Shift' && !e.ctrlKey && !e.altKey) {
        e.preventDefault();
        this.checkOpType('move');
    }
    else if (e.key === 'f' || e.key === 'F') {
        e.preventDefault();
        this.checkOpType('box');
    }
    else if (e.key === 'd' || e.key === 'D') {
        e.preventDefault();
        this.checkOpType('polygon');
    }
    else if (e.key === 'q' || e.key === 'Q') {
        e.preventDefault();
        this.checkOpType('select');
    }
    else if (e.key === 'a' || e.key === 'K') {
        this.checkOpType('none');
    }
    __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
    __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLocation).call(this);
}, _CanvasLabel_keyUpEvent = function _CanvasLabel_keyUpEvent(e) {
    if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f")) {
        return;
    }
    // const cvs = this.#canvas
    if (e.key === 'Shift') {
        this.returnOpType();
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawLocation).call(this);
    }
}, _CanvasLabel_addDrawing = function _CanvasLabel_addDrawing(et) {
    if (!__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    let x = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cx;
    let y = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cy;
    if (__classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown && __classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'box') {
        /* 有数据才能够进行画框 */
        if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f")) {
            let w = x - __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data[0];
            let h = y - __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data[1];
            if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length === 4) {
                __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.splice(__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length - 2, 2, w, h);
            }
            else {
                __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.push(w, h);
            }
        }
        else {
            let d = {
                id: new Date().getTime(),
                /* 数据解构避免浅拷贝影响 */
                labelTypeIds: [...__classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId],
                type: 'box',
                data: []
            };
            d.data.push(x, y);
            __classPrivateFieldSet(this, _CanvasLabel_labelIngData, d, "f");
        }
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'polygon') {
        if (et === 'click') {
            if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f")) {
                // 如果有数据的话就添加
                __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.push(x, y);
            }
            else {
                // 没有数据则初始化数据
                let d = {
                    id: new Date().getTime(),
                    /* 数据解构避免浅拷贝影响 */
                    labelTypeIds: [...__classPrivateFieldGet(this, _CanvasLabel_store, "f").useLabelTypeId],
                    type: 'polygon',
                    data: []
                };
                d.data.push(x, y);
                __classPrivateFieldSet(this, _CanvasLabel_labelIngData, d, "f");
            }
        }
        else if (et === 'mousemove') {
            if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f") && __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length > 2) {
                __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.splice(__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length - 2, 2, x, y);
            }
            else if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f") && __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length === 2) {
                __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.push(x, y);
            }
        }
    }
}, _CanvasLabel_addDrawingEnd = function _CanvasLabel_addDrawingEnd(et) {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'box' && et === 'mouseup') {
        if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f")) {
            __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").push(__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f"));
        }
        __classPrivateFieldSet(this, _CanvasLabel_labelIngData, null, "f");
    }
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'polygon' && et === 'dblclick') {
        if (__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f")) {
            __classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.splice(__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f").data.length - 4, 4);
            __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").push(__classPrivateFieldGet(this, _CanvasLabel_labelIngData, "f"));
        }
        __classPrivateFieldSet(this, _CanvasLabel_labelIngData, null, "f");
    }
}, _CanvasLabel_getLabelSetting = function _CanvasLabel_getLabelSetting(ids) {
    var _a;
    let r = [];
    if ((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _a === void 0 ? void 0 : _a.labelTypeList) {
        let ll = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").labelTypeList;
        for (let l of ll) {
            for (let id of ids) {
                if (id === l.id) {
                    r.push(l);
                    break;
                }
            }
            if (r.length == ids.length) {
                break;
            }
        }
    }
    return r;
}, _CanvasLabel_drawBox = function _CanvasLabel_drawBox(d, index) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    if (!__classPrivateFieldGet(this, _CanvasLabel_ctx, "f")) {
        return;
    }
    let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    ctx.save();
    let setting = __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_getLabelSetting).call(this, d.labelTypeIds);
    if (setting.length === 0) {
        ctx.restore();
        return;
    } /* 判断是否处于选择状态 */
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").indexOf(d.id) >= 0) {
        ctx.fillStyle = ((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _a === void 0 ? void 0 : _a.fillStyle) || '#0008';
        ctx.strokeStyle = ((_b = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _b === void 0 ? void 0 : _b.strokeStyle) || '#000';
        ctx.lineWidth = ((_c = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _c === void 0 ? void 0 : _c.strokeWidth) || 6;
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_selectIngLabelIds, "f").indexOf(d.id) >= 0) {
        ctx.fillStyle = ((_d = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _d === void 0 ? void 0 : _d.fillStyle) || '#0008';
        ctx.strokeStyle = ((_e = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _e === void 0 ? void 0 : _e.strokeStyle) || '#000';
        ctx.lineWidth = ((_f = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _f === void 0 ? void 0 : _f.strokeWidth) || 6;
    }
    else {
        ctx.fillStyle = (setting === null || setting === void 0 ? void 0 : setting[0].fillStyle) || '#0008';
        ctx.strokeStyle = (setting === null || setting === void 0 ? void 0 : setting[0].strokeStyle) || '#000';
        ctx.lineWidth = (setting === null || setting === void 0 ? void 0 : setting[0].strokeWidth) || 2;
    }
    ctx.lineCap = 'round';
    ctx.lineJoin = "round";
    ctx.fillRect(d.data[0], d.data[1], d.data[2], d.data[3]);
    ctx.strokeRect(d.data[0], d.data[1], d.data[2], d.data[3]);
    ctx.fillStyle = (setting === null || setting === void 0 ? void 0 : setting[0].fontFillStyle) || '#0008';
    ctx.font = (setting === null || setting === void 0 ? void 0 : setting[0].font) || "15px '微软雅黑'";
    ctx.textAlign = 'center';
    let tag = '';
    if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'name') {
        setting.forEach((st) => {
            tag += st.name + " ";
        });
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'number') {
        tag = index.toString();
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'id') {
        tag = ((_g = d.id) === null || _g === void 0 ? void 0 : _g.toString()) || '无id';
    }
    tag = tag.trim();
    ctx.fillText(tag || '未定义标签', (d.data[0] + d.data[0] + d.data[2]) / 2, d.data[1] - 5);
    ctx.restore();
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").indexOf(d.id) >= 0) {
        let l = getLabelData(d);
        for (let i = 0; i < l.length / 2; i++) {
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_dpi).call(this, ctx, l[i * 2], l[(i * 2) + 1], (((_h = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _h === void 0 ? void 0 : _h.strokeWidth) || 6) / 2);
        }
    }
}, _CanvasLabel_dp = function _CanvasLabel_dp(ctx, d) {
    ctx.beginPath();
    let l = d.data.length / 2;
    let minx = 0;
    let miny = 0;
    let maxx = 0;
    for (let i = 0; i < l; i++) {
        if (i == 0) {
            ctx.moveTo(d.data[0], d.data[1]);
            maxx = minx = d.data[0];
            miny = d.data[1];
        }
        else {
            let ai = i * 2;
            if (d.data[ai] < minx) {
                minx = d.data[ai];
            }
            else if (d.data[ai] > maxx) {
                maxx = d.data[ai];
            }
            if (d.data[ai + 1] < miny) {
                miny = d.data[ai + 1];
            }
            ctx.lineTo(d.data[ai], d.data[ai + 1]);
        }
    }
    ctx.closePath();
    return { minx, miny, maxx };
}, _CanvasLabel_drawPath = function _CanvasLabel_drawPath(d, index) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    if (!__classPrivateFieldGet(this, _CanvasLabel_ctx, "f")) {
        return;
    }
    let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    ctx.save();
    let st = __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_getLabelSetting).call(this, d.labelTypeIds);
    if (st.length === 0) {
        ctx.restore();
        return;
    }
    // 绘制路径不渲染
    let { minx, miny, maxx } = __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_dp).call(this, ctx, d);
    // 加载设置
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").indexOf(d.id) >= 0) {
        ctx.fillStyle = ((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _a === void 0 ? void 0 : _a.fillStyle) || '#0008';
        ctx.strokeStyle = ((_b = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _b === void 0 ? void 0 : _b.strokeStyle) || '#000';
        ctx.lineWidth = ((_c = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _c === void 0 ? void 0 : _c.strokeWidth) || 6;
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_selectIngLabelIds, "f").indexOf(d.id) >= 0) {
        ctx.fillStyle = ((_d = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _d === void 0 ? void 0 : _d.fillStyle) || '#0008';
        ctx.strokeStyle = ((_e = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _e === void 0 ? void 0 : _e.strokeStyle) || '#000';
        ctx.lineWidth = ((_f = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _f === void 0 ? void 0 : _f.strokeWidth) || 6;
    }
    else {
        ctx.fillStyle = (st === null || st === void 0 ? void 0 : st[0].fillStyle) || '#0008';
        ctx.strokeStyle = (st === null || st === void 0 ? void 0 : st[0].strokeStyle) || '#000';
        ctx.lineWidth = (st === null || st === void 0 ? void 0 : st[0].strokeWidth) || 2;
    }
    ctx.lineCap = 'round';
    ctx.lineJoin = "round";
    ctx.stroke();
    ctx.fill();
    ctx.fillStyle = (st === null || st === void 0 ? void 0 : st[0].fontFillStyle) || '#0008';
    ctx.font = (st === null || st === void 0 ? void 0 : st[0].font) || "15px '微软雅黑'";
    ctx.textAlign = 'center';
    let tag = "";
    if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'name') {
        st.forEach((s) => {
            tag += s.name + " ";
        });
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'number') {
        tag = index.toString();
    }
    else if (__classPrivateFieldGet(this, _CanvasLabel_setting, "f").showLabelTagType === 'id') {
        tag = ((_g = d.id) === null || _g === void 0 ? void 0 : _g.toString()) || '无id';
    }
    tag = tag.trim();
    ctx.fillText(tag || '未定义标签', (maxx + minx) / 2, miny - 5);
    ctx.restore();
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").indexOf(d.id) >= 0) {
        let l = getLabelData(d);
        for (let i = 0; i < l.length / 2; i++) {
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_dpi).call(this, ctx, l[i * 2], l[(i * 2) + 1], (((_h = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectIngStyle) === null || _h === void 0 ? void 0 : _h.strokeWidth) || 6) / 2);
        }
    }
}, _CanvasLabel_dpi = function _CanvasLabel_dpi(ctx, x, y, r) {
    var _a;
    ctx.save();
    ctx.beginPath();
    ctx.arc(x, y, r, 0, 2 * Math.PI);
    ctx.fillStyle = ((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _a === void 0 ? void 0 : _a.pointFillStyle) || 'black';
    ctx.fill();
    ctx.closePath();
    ctx.restore();
}, _CanvasLabel_drawPoint = function _CanvasLabel_drawPoint() {
    var _a, _b, _c;
    if (!__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    if (__classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f").length === 0) {
        return;
    }
    let d = __classPrivateFieldGet(this, _CanvasLabel_backData, "f");
    let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    let r = (((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _a === void 0 ? void 0 : _a.pointFillR) || 6) / d.zoom;
    if (r < (((_b = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _b === void 0 ? void 0 : _b.pointFillR) || 6)) {
        r = ((_c = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _c === void 0 ? void 0 : _c.pointFillR) || 6;
    }
    let x = __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[1];
    let y = __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[2];
    __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_dpi).call(this, ctx, x, y, r);
}, _CanvasLabel_drawLocation = function _CanvasLabel_drawLocation() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    if (!__classPrivateFieldGet(this, _CanvasLabel_canvas, "f") || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || __classPrivateFieldGet(this, _CanvasLabel_opType, "f") === 'move') {
        return;
    }
    let x = __classPrivateFieldGet(this, _CanvasLabel_store, "f").x;
    let y = __classPrivateFieldGet(this, _CanvasLabel_store, "f").y;
    let cx = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cx;
    let cy = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cy;
    const cvs = __classPrivateFieldGet(this, _CanvasLabel_canvas, "f");
    const ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    ctx.save();
    ctx.lineWidth = ((_b = (_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _a === void 0 ? void 0 : _a.sightBead) === null || _b === void 0 ? void 0 : _b.coordinatesWidth) || 2;
    ctx.strokeStyle = ((_d = (_c = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _c === void 0 ? void 0 : _c.sightBead) === null || _d === void 0 ? void 0 : _d.coordinatesColor) || '#fff7';
    ctx.beginPath();
    ctx.moveTo(x, 0);
    ctx.lineTo(x, cvs.height);
    ctx.closePath();
    ctx.moveTo(0, y);
    ctx.lineTo(cvs.width, y);
    ctx.globalCompositeOperation = 'difference';
    ctx.stroke();
    ctx.restore();
    ctx.save();
    ctx.font = ((_f = (_e = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _e === void 0 ? void 0 : _e.sightBead) === null || _f === void 0 ? void 0 : _f.fontStyle) || '10 "微软雅黑"';
    ctx.fillStyle = ((_h = (_g = __classPrivateFieldGet(this, _CanvasLabel_setting, "f")) === null || _g === void 0 ? void 0 : _g.sightBead) === null || _h === void 0 ? void 0 : _h.fontColor) || '#fff';
    ctx.textAlign = 'center';
    ctx.globalCompositeOperation = 'difference';
    // 绘制坐标文本
    ctx.fillText(`x = ${cx.toFixed(2)}px , y = ${cy.toFixed(2)}px`, x - 100, y - 8);
    ctx.fillText(`x = ${cx.toFixed(2)}px , y = ${cy.toFixed(2)}px`, x + 100, y - 8);
    ctx.restore();
}, _CanvasLabel_drawMove = function _CanvasLabel_drawMove(x, y) {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== 'move') {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPipeLine).call(this);
        return;
    }
    this.translate(x, y);
}, _CanvasLabel_drawLabel = function _CanvasLabel_drawLabel(l, index) {
    if (__classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedHidden).call(this, l)) {
        return;
    }
    if (l.type === 'box') {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawBox).call(this, l, index);
    }
    else if (l.type === 'polygon') {
        __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_drawPath).call(this, l, index);
    }
}, _CanvasLabel_labelNeedHidden = function _CanvasLabel_labelNeedHidden(l) {
    if (this.labelIsHidden(l.id)) {
        return true;
    }
    for (let id of l.labelTypeIds) {
        if (this.labelTypeIsHidden(id)) {
            return true;
        }
    }
    return false;
}, _CanvasLabel_labelNeedSelect = function _CanvasLabel_labelNeedSelect(et) {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== "select" || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    // 检查现在是否在编辑点 如果是的话 就不用进行选择
    if (__classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f").length > 0) {
        return;
    }
    const ls = __classPrivateFieldGet(this, _CanvasLabel_labelList, "f");
    let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
    ctx.save();
    let d = __classPrivateFieldGet(this, _CanvasLabel_backData, "f");
    ctx.transform(d.zoom, 0, 0, d.zoom, d.mx, d.my);
    __classPrivateFieldSet(this, _CanvasLabel_selectIngLabelIds, [], "f");
    for (let i = ls.length - 1; i >= 0; i--) {
        ctx.save();
        if (__classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedHidden).call(this, ls[i])) {
            ctx.restore();
            continue;
        }
        if (ls[i].type === 'box') {
            // 绘制矩形 不渲染
            ctx.rect(ls[i].data[0], ls[i].data[1], ls[i].data[2], ls[i].data[3]);
        }
        else if (ls[i].type === 'polygon') {
            // 绘制路径不渲染
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_dp).call(this, ctx, ls[i]);
        }
        // 检查坐标是否在路径内
        if (ctx.isPointInPath(__classPrivateFieldGet(this, _CanvasLabel_store, "f").x, __classPrivateFieldGet(this, _CanvasLabel_store, "f").y)) {
            if (et === 'ing') {
                __classPrivateFieldSet(this, _CanvasLabel_selectIngLabelIds, [ls[i].id], "f");
            }
            else if (et === 'entry') {
                this.selectLabels([ls[i].id], false);
            }
            ctx.restore();
            ctx.restore();
            return;
        }
        ctx.restore();
    }
    ctx.restore();
}, _CanvasLabel_atPointRound = function _CanvasLabel_atPointRound(ctx, l, d, draw = false) {
    var _a, _b, _c;
    //  将标签数据取出转换为number[]
    let p = getLabelData(l);
    let r = (((_a = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _a === void 0 ? void 0 : _a.pointFillR) || 6) / d.zoom;
    if (r < (((_b = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _b === void 0 ? void 0 : _b.pointFillR) || 6)) {
        r = ((_c = __classPrivateFieldGet(this, _CanvasLabel_setting, "f").selectStyle) === null || _c === void 0 ? void 0 : _c.pointFillR) || 6;
    }
    for (let i = 0; i < p.length / 2; i++) {
        ctx.save();
        ctx.beginPath();
        ctx.arc(p[i * 2], p[(i * 2) + 1], r, 0, 2 * Math.PI);
        ctx.closePath();
        if (ctx.isPointInPath(__classPrivateFieldGet(this, _CanvasLabel_store, "f").x, __classPrivateFieldGet(this, _CanvasLabel_store, "f").y)) {
            __classPrivateFieldSet(this, _CanvasLabel_canEditPoint, [i, p[i * 2], p[i * 2 + 1]], "f");
            __classPrivateFieldSet(this, _CanvasLabel_selectIngLabelIds, [], "f");
            ctx.restore();
            return i;
        }
        ctx.restore();
    }
    return false;
}, _CanvasLabel_selectLabelPoint = function _CanvasLabel_selectLabelPoint() {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== "select" || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    // 检查现在是否处于多选状态 或无选择 是的话就不能进行点位选择
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").length > 1 || __classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").length == 0) {
        return;
    }
    if (__classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown) {
        return;
    }
    // 重置当前选择的点
    __classPrivateFieldSet(this, _CanvasLabel_canEditPoint, [], "f");
    for (let l of __classPrivateFieldGet(this, _CanvasLabel_labelList, "f")) {
        if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f")[0] === l.id) {
            if (__classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_labelNeedHidden).call(this, l)) {
                return;
            }
            let ctx = __classPrivateFieldGet(this, _CanvasLabel_ctx, "f");
            ctx.save();
            let d = __classPrivateFieldGet(this, _CanvasLabel_backData, "f");
            ctx.transform(d.zoom, 0, 0, d.zoom, d.mx, d.my);
            __classPrivateFieldGet(this, _CanvasLabel_instances, "m", _CanvasLabel_atPointRound).call(this, ctx, l, d);
            ctx.restore();
            return;
        }
    }
}, _CanvasLabel_moveSelectLabel = function _CanvasLabel_moveSelectLabel(x, y) {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== "select" || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    // 检查现在是否是在移动点位 有没有被选择的框
    if (!__classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown || __classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").length === 0 || __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f").length > 0) {
        return;
    }
    let b = __classPrivateFieldGet(this, _CanvasLabel_backData, "f");
    x = x / b.zoom;
    y = y / b.zoom;
    this.moveSelectLabels(x, y, false);
}, _CanvasLabel_moveLabelPoint = function _CanvasLabel_moveLabelPoint() {
    if (__classPrivateFieldGet(this, _CanvasLabel_opType, "f") !== "select" || !__classPrivateFieldGet(this, _CanvasLabel_ctx, "f") || !__classPrivateFieldGet(this, _CanvasLabel_backData, "f")) {
        return;
    }
    if (!__classPrivateFieldGet(this, _CanvasLabel_store, "f").mouseIsDown) {
        return;
    }
    if (__classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f").length == 1 && __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f").length > 0) {
        __classPrivateFieldGet(this, _CanvasLabel_labelList, "f").forEach((l) => {
            if (l.id === __classPrivateFieldGet(this, _CanvasLabel_selectLabelIds, "f")[0]) {
                let cx = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cx;
                let cy = __classPrivateFieldGet(this, _CanvasLabel_store, "f").cy;
                if (l.type === 'box') {
                    let x = l.data[0];
                    let y = l.data[1];
                    let w = l.data[2];
                    let h = l.data[3];
                    // 根据不同点位调整元数据坐标
                    switch (__classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[0]) {
                        case 0:
                            l.data[0] = cx;
                            l.data[1] = cy;
                            l.data[2] = (x + w) - cx;
                            l.data[3] = (y + h) - cy;
                            break;
                        case 1:
                            l.data[0] = x;
                            l.data[1] = cy;
                            l.data[2] = cx - x;
                            l.data[3] = (y - cy) + h;
                            break;
                        case 2:
                            l.data[0] = x;
                            l.data[1] = y;
                            l.data[2] = cx - x;
                            l.data[3] = cy - y;
                            break;
                        case 3:
                            l.data[0] = cx;
                            l.data[1] = y;
                            l.data[2] = (x - cx) + w;
                            l.data[3] = cy - y;
                            break;
                    }
                }
                else if (l.type === 'polygon') {
                    l.data[(__classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[0] * 2)] = cx;
                    l.data[(__classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[0] * 2) + 1] = cy;
                }
                __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[1] = cx;
                __classPrivateFieldGet(this, _CanvasLabel_canEditPoint, "f")[2] = cy;
            }
        });
    }
};
export { CanvasLabel, deepCopy };
